package com.hbzhit.modules.sys.controller;

import cn.hutool.core.bean.BeanUtil;
import com.hbzhit.common.annotation.SysLog;
import com.hbzhit.common.extension.BaseController;
import com.hbzhit.common.utils.R;
import com.hbzhit.common.utils.ShiroUtils;
import com.hbzhit.common.validator.Assert;
import com.hbzhit.modules.sys.entity.SysUserEntity;
import com.hbzhit.modules.sys.service.SysUserRoleService;
import com.hbzhit.modules.sys.service.SysUserService;
import com.hbzhit.modules.sys.vo.PasswordVO;
import com.hbzhit.modules.sys.vo.SysUserUpdateBatchVO;
import com.hbzhit.modules.wx.service.WxUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统用户
 *
 * @author Mark sunlightcs@gmail.com
 */
@Api(tags = {"用户管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends BaseController<SysUserService, SysUserEntity> {
	private final SysUserRoleService sysUserRoleService;

	@Resource
	private WxUserService wxUserService;

	/**
	 * 获取用户所属的角色列表
	 */
	@ApiOperation(value = "获取用户所属的角色列表")
	@GetMapping("/roleIdList/{userId}")
	public R<List<Long>> userInfo(@PathVariable("userId") Long userId){
		List<Long> roleIdList = sysUserRoleService.queryRoleIdList(userId);

		return R.ok(roleIdList);
	}

	/**
	 * 获取登录的用户信息
	 */
	@ApiOperation(value = "获取登录用户的信息")
	@GetMapping("/info")
	public R<SysUserEntity> info(){
		SysUserEntity currUser = ShiroUtils.getUser();
		SysUserEntity dbUser = this.baseService.getById(currUser.getId());
		BeanUtil.copyProperties(dbUser, currUser);
		return R.ok(currUser);
	}

	/**
	 * 修改登录用户密码
	 */
	@ApiOperation(value = "修改用户密码")
	@SysLog("修改密码")
	@PostMapping("/password")
	public R<Object> password(@RequestBody PasswordVO form){
		Assert.isBlank(form.getNewPassword(), "新密码不为能空");

		SysUserEntity user = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
		//sha256加密
		String password = new Sha256Hash(form.getPassword(), user.getSalt()).toHex();
		//sha256加密
		String newPassword = new Sha256Hash(form.getNewPassword(), user.getSalt()).toHex();

		//更新密码
		boolean flag = baseService.updatePassword(user.getId(), password, newPassword);
		if(!flag){
			return R.error("原密码不正确");
		}

		return R.ok();
	}

	/**
	 * 删除用户
	 */
	@ApiOperation(value = "删除用户")
	@SysLog("删除用户")
	@PostMapping("/delete-user")
	@RequiresPermissions("sys:user:delete")
	public R<Object> deleteUser(@RequestBody Long[] userIds){
		baseService.deleteBatchUser(userIds);
		return R.ok();
	}

	/**
	 * 批量更新
	 */
	@ApiOperation(value = "批量更新")
	@SysLog("批量更新")
	@PostMapping("/update-batch")
	@RequiresPermissions("sys:user:update")
	public R<Object> updateBatch(@RequestBody SysUserUpdateBatchVO form){
		baseService.updateBatch(form.getUser(), form.getUserIds());

		return R.ok();
	}


	@GetMapping("/peopleSync")
	@ApiOperation(("同步企业微信人员和部门信息"))
	public R<Object> peopleSync() {
		if (wxUserService.peopleSync()) return R.ok("同步成功！");
		else return R.error("同步失败！");
	}

}
